﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

using BitcoinCommon;


namespace BitcoinExchange.Market
{
    public class KorbitOrder
    {
        MainInfo _info;

        public KorbitOrder(MainInfo info)
        {
            _info = info;
        }

        public string OrderBuy(string strBTCPrice, string strBTCVolume)
        {
            string strReturn = "";
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면
            if (!_info.IsLogin())
                return strReturn;

            Dictionary<string, string> dicURLParameter = _info.GetKorbitURLParameterAccessKey();
            dicURLParameter.Add("type", "limit");
            dicURLParameter.Add("price", strBTCPrice);
            dicURLParameter.Add("coin_amount", strBTCVolume);
            dicURLParameter.Add("fiat_amount", "");

            //접속 주소를 입력한다.
            strURL = _info.GetKorbitURL() + StringURL.URL_KORBIT_ORDER_BUY;
            strParameters = _info.ConvertURLParameter(dicURLParameter);

            strData = _info.RequestData(strURL, strParameters);

            if (strData.Length == 0)
                return strReturn;

            //Order ID 추출
            try
            {
                JObject objItem = JObject.Parse(strData);
                strReturn = objItem["orderId"].ToString();
            }
            catch (Exception e)
            {
                strReturn = "";
                Console.WriteLine(e.Message);
            }
            
            return strReturn;
        }

        public string OrderSell(string strBTCPrice, string strBTCVolume)
        {
            string strReturn = "";
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면
            if (!_info.IsLogin())
                return strReturn;

            string strOrder = "limit";

            
            strOrder = "limit";

            Dictionary<string, string> dicURLParameter = _info.GetKorbitURLParameterAccessKey();
            dicURLParameter.Add("type", strOrder);
            dicURLParameter.Add("price", strBTCPrice);
            dicURLParameter.Add("coin_amount", strBTCVolume);

            //접속 주소를 입력한다.
            strURL = _info.GetKorbitURL() + StringURL.URL_KORBIT_ORDER_SELL;
            strParameters = _info.ConvertURLParameter(dicURLParameter);

            strData = _info.RequestData(strURL, strParameters);

            if (strData.Length == 0)
                return strReturn;

            //Order ID 추출
            try
            {
                JObject objItem = JObject.Parse(strData);
                strReturn = objItem["orderId"].ToString();
            }
            catch (Exception e)
            {
                strReturn = "";
                Console.WriteLine(e.Message);
            }

            return strReturn;
        }

        public string OrderCancel(string strOrderID)
        {
            string strReturn = "";
            string strData = "";
            string strURL = "";
            string strParameters = "";


            //로그인 하지 않았다면
            if (!_info.IsLogin())
                return strReturn;

            Dictionary<string, string> dicURLParameter = _info.GetKorbitURLParameterAccessKey();
            dicURLParameter.Add("id", strOrderID);

            strURL = _info.GetKorbitURL() + StringURL.URL_KORBIT_ORDER_CANCEL;
            strParameters = _info.ConvertURLParameter(dicURLParameter);
            strData = _info.RequestData(strURL, strParameters);

            if (strData.Length == 0)
                return strReturn;

            //Order ID 추출
            try
            {
                JArray arrItems = JArray.Parse(strData);
                foreach (JToken oItem in arrItems)
                {
                    strReturn = oItem["orderId"].ToString();
                }
            }
            catch (Exception e)
            {
                strReturn = "";
                Console.WriteLine(e.Message);
            }

            return strReturn;
        }
    }
}
